
function walk(ast, { enter, leave }) {
    visit(ast, null, enter, leave)
}
/**
 * 访问一个语法树的节点
 * @param {*} node  当前节点
 * @param {*} parent  父节点
 * @param {*} enter  进入时方法
 * @param {*} leave 离开时方法
 */
function visit(node, parent, enter, leave) {
    if(enter){
        enter.call(null,node,parent)
    }
    let childKeys = Object.keys(node).filter(key => typeof node[key] === 'object');
    childKeys.forEach(childKey =>{
        let value = node[childKey]
        if(Array.isArray(value)){
            value.forEach(val=>visit(val, node, enter, leave))
        }else if(value && value.type ){
            visit(value, node, enter, leave);
        }
    })
    if (leave) {
        leave(node, parent)
    }
}

module.exports = walk